import { Controller, Get, Put, Body, UseGuards, Request } from '@nestjs/common';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { RolesGuard } from './guards/roles.guard';
import { Roles } from './decorators/roles.decorator';
import { AuthService } from './auth.service';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
@ApiTags('用户管理')
@Controller('api/users')
@UseGuards(JwtAuthGuard, RolesGuard)
export class UserController {
  constructor(private readonly authService: AuthService) {}

  @Get('profile')
  @ApiOperation({ summary: '获取当前用户信息' })
  @ApiResponse({ status: 200, description: '返回用户信息' })
  async getProfile(@Request() req: any) {
    const user = await this.authService.validateUser(req.user.sub);
    return {
      id: user.id,
      email: user.email,
      name: user.name,
      role: user.role,
    };
  }

  @Put('role')
  @Roles('ADMIN')
  @ApiOperation({ summary: '更新用户角色（仅管理员）' })
  @ApiResponse({ status: 200, description: '角色更新成功' })
  async updateUserRole(@Body() body: { userId: string; role: string }, @Request() req: any) {
    const updatedUser = await this.authService.updateUserRole(
      body.userId,
      body.role,
      req.user.sub
    );
    return { message: '角色更新成功', user: updatedUser };
  }
} 